package com.zzq.my.jetpacks.jectpackapp.ui.stickyheaderrecyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yuyang.stickyheaders.AdapterDataProvider
import com.zzq.my.jetpacks.jectpackapp.R

/*
  * ================================================
  * 描述： 需要实现 AdapterDataProvider 接口，并在 getAdapterData() 返回 model 数据，用于判断对应 position 是否为 Header
  * 作者：zhuangzeqin
  * 时间: 2023/6/28 11:04
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
class StickyHeaderRecyclerAdapter : RecyclerView.Adapter<BaseViewHolder>(), AdapterDataProvider {
    private val dataList: MutableList<Any> = ArrayList()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        //根据类型创建不同的viewHolder
        return if (viewType == 0) {
            ItemViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
            )
        } else {
            HeaderViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.header_view, parent, false)
            )
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val item = dataList[position]
        if (item is Item) {//内容
            val itemViewHolder = holder as ItemViewHolder
            itemViewHolder.titleTextView.text = item.title
            itemViewHolder.messageTextView.text = item.message
        } else if (item is HeaderItem) {//标题
            val headerViewHolder = holder as HeaderViewHolder
            headerViewHolder.titleTextView.text = item.title
            headerViewHolder.button.setTextColor(item.color)
            headerViewHolder.button.setOnClickListener {
                if (item.color === -0xafb0) {
                    item.color = -0x888889
                } else {
                    item.color = -0xafb0
                }
                notifyItemChanged(position)
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun getItemViewType(position: Int): Int {//类型
        return if (dataList[position] is Item) 0 else 1
    }

    /**
     * todo 并在 getAdapterData() 返回 model 数据，用于判断对应 position 是否为 Header
     */
    override fun getAdapterData(): List<*> {
        return dataList
    }

    fun setDataList(items: List<Any>?) {
        dataList.clear()
        dataList.addAll(items ?: emptyList())
        notifyDataSetChanged()
    }

    fun addDataList(items: List<Any>?) {
        if (items != null) {
            val start = dataList.size
            dataList.addAll(items)
            notifyItemRangeInserted(start, items.size)
        }
    }

    //todo 内容的ViewHolder
    private class ItemViewHolder constructor(itemView: View) :
        BaseViewHolder(itemView) {
        var titleTextView: TextView
        var messageTextView: TextView

        init {
            titleTextView = itemView.findViewById(R.id.tv_title)
            messageTextView = itemView.findViewById(R.id.tv_message)
        }
    }

    //todo 头部的ViewHolder
    private class HeaderViewHolder constructor(itemView: View) :
        BaseViewHolder(itemView) {
        var titleTextView: TextView
        var button: TextView

        init {
            titleTextView = itemView.findViewById(R.id.tv_title)
            button = itemView.findViewById(R.id.button)
        }
    }


}

open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

